package com.jdd.modules.mqtt.server.revice;

import com.alibaba.fastjson.JSONObject;
import com.jdd.common.util.OssUtil;
import com.jdd.dao.entity.RuiKaSnapResponse;
import com.jdd.dao.entity.RuiKaTDeviceFaceDiscernLog;
import com.jdd.dao.service.RuiKaSnapResponseService;
import com.jdd.dao.service.RuiKaTDeviceFaceDiscernLogService;
import com.jdd.dao.service.TUserPassMediumService;
import com.jdd.dao.service.TVisitPassMediumService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @program: jdd-people-passage
 * @ClassName ReciveMqttQrResponseService
 * @description: 人脸识别结果topic消息处理类
 * @author: zsj
 * @create: 2022-03-24 09:56
 * @Version 1.0
 **/
@Service("ruikamqttFaceSnapResponseStrategy")
public class RuiKaReciveMqttSnapFaceResponseServiceStrategy implements IReciveMqttBaseStrategy {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private RuiKaSnapResponseService ruiKaSnapResponseService;
    @Autowired
    private TVisitPassMediumService visitPassMediumService;
    @Autowired
    private TUserPassMediumService userPassMediumService;
    @Autowired
    private OssUtil ossUtil;

    /**
     * 处理从mqtt接收到的人脸识别结果消息接口
     *
     * @param message 消息
     * @return void
     * @Description
     * @Author zsj
     * @Date 2022-03-24 9:53
     **/
    @Override
    public void handleMessage(String message) {
        logger.info("接收到mqtt陌生人抓拍消息，消息：{}", message);
        JSONObject jsonObject = JSONObject.parseObject(message);
        JSONObject body = jsonObject.getJSONObject("info");
        RuiKaSnapResponse ruiKaSnapResponse = new RuiKaSnapResponse();
        ruiKaSnapResponse.setFacesluiceId(body.getString("facesluiceId"));
        ruiKaSnapResponse.setFacesluiceName(body.getString("facesluiceName"));
        ruiKaSnapResponse.setDirection(body.getString("direction"));
        ruiKaSnapResponse.setLeft(body.getString("left"));
        ruiKaSnapResponse.setTop(body.getString("top"));
        ruiKaSnapResponse.setRight(body.getString("right"));
        ruiKaSnapResponse.setBottom(body.getString("bottom"));
        ruiKaSnapResponse.setTime(body.getString("time"));
        ruiKaSnapResponse.setPushType(body.getString("PushType"));
        ruiKaSnapResponse.setTemperature(body.getString("temperature"));
        ruiKaSnapResponse.setTemperatureAlarm(body.getString("temperatureAlarm"));
        ruiKaSnapResponse.setTemperature(body.getString("temperatureMode"));
        ruiKaSnapResponse.setIsNoMask(body.getString("isNoMask"));
        if (StringUtils.isNotBlank(body.getString("pic"))) {
            //上传阿里云
            String faceUrl = null;
            try {
                faceUrl = ossUtil.uploadBaseString2OSS(body.getString("pic"));
            } catch (IOException e) {
                logger.error("上传人脸图片失败：" + e.getMessage());
                e.printStackTrace();
            }
            ruiKaSnapResponse.setPic(faceUrl);
        }
        ruiKaSnapResponseService.save(ruiKaSnapResponse);
    }
}